package chpt_06_03_command;

/**
 * @author Ping
 * @date 2023/5/18 8:46
 **/
public class CommandDemo {

    public static void main(String args[]) {


        TVControllerForStu tvControllerForStu = new TVControllerForStu() ;
        tvControllerForStu.tvOn();
        tvControllerForStu.tvOff() ;
        TVControllerForParent tvControllerForParent = new TVControllerForParent();
        tvControllerForParent.tvOn() ;
        tvControllerForParent.tvChange();
        tvControllerForParent.tvOff();



    }


}


/**
 * Resources/Receivor role
 */

class TV {

    public void on(){
        System.out.println("TV was ON.") ;
    }
    public void off() {
        System.out.println("TV was switch OFF.") ;
    }
    public void change() {
        System.out.println("TV channel was changed ") ;
    }
}

/**
 * Commmand:
 */

interface Command {
    public void execute() ;
}

/**
 * Concrete Command/ Concrete Request
 */

class TVOn implements Command {
    /**
     * related resource/receivor/executor
     */
    private TV tv ;

    public TVOn() {
        tv = new TV() ;
    }

    @Override
    public void execute() {
        tv.on();
    }
}

class TVOff implements Command {

    /**
     * related resource/receivor/executor
     */
    private TV tv ;

    public TVOff() {
        tv = new TV() ;
    }

    @Override
    public void execute() {
        tv.off();
    }
}

class ChannelChange implements Command {
    /**
     * related resource/receivor/executor
     */
    private TV tv ;

    public ChannelChange() {
        tv = new TV() ;
    }

    @Override
    public void execute() {
        tv.change();
    }
}
/**
 * Use commands based on real requirement
 */

class TVControllerForParent {
    private TVOn tvOn ;
    private TVOff tvOff ;
    private ChannelChange channelChange ;

    public TVControllerForParent() {
        tvOn = new TVOn() ;
        tvOff = new TVOff() ;
        channelChange = new ChannelChange() ;
    }

    public void tvOn() {
        tvOn.execute();

    }

    public void tvOff() {
        tvOff.execute();
    }
    public void tvChange() {
        channelChange.execute();
    }
}

class TVControllerForStu {
    private TVOn tvOn ;
    private TVOff tvOff ;

    public TVControllerForStu() {
        tvOn = new TVOn() ;
        tvOff = new TVOff() ;
    }

    public void tvOn() {
        tvOn.execute();

    }

    public void tvOff() {
        tvOff.execute();
    }

}










